const express = require('express');
const router = express.Router();
const {executeQueryAsync} = require("../db/execute");

// 添加购物车
router.post('/add', async (req, res, next) => {
    const { userId, goodsId, countNbr } = req.body;

    // 校验 countNbr 是否大于0
    if (countNbr <= 0) {
        return res.status(400).json({ code: 700101, message: 'countNbr 必须大于0', data: null });
    }

    // 校验用户是否存在
    const checkUserQuery = 'SELECT * FROM sys_user WHERE user_id = ?';
    const checkUserRes = await executeQueryAsync(checkUserQuery, [userId]);
    if (checkUserRes.results.length === 0) {
        return res.status(400).json({ code: 600101, message: '用户不存在', data: null });
    }

    // 校验商品是否存在
    const checkGoodsQuery = 'SELECT * FROM goods WHERE goods_id = ?';
    const checkGoodsRes = await executeQueryAsync(checkGoodsQuery, [goodsId]);
    if (checkGoodsRes.results.length === 0) {
        return res.status(400).json({ code: 800101, message: '商品不存在', data: null });
    }

    // 查询购物车中是否已存在该商品
    const checkCartQuery = 'SELECT * FROM cart WHERE user_id = ? AND goods_id = ?';
    const checkCartRes = await executeQueryAsync(checkCartQuery, [userId, goodsId]);
    if (checkCartRes.results.length > 0) {
        // 如果存在，则更新数量
        const updateQuery = 'UPDATE cart SET count_nbr = ? WHERE user_id = ? AND goods_id = ?';
        await executeQueryAsync(updateQuery, [countNbr, userId, goodsId]);
        res.status(200).json({ code: 200, message: '更新购物车商品数量成功', data: null });
    } else {
        // 如果不存在，则插入新数据
        const insertQuery = 'INSERT INTO cart (user_id, goods_id, count_nbr) VALUES (?, ?, ?)';
        await executeQueryAsync(insertQuery, [userId, goodsId, countNbr]);
        res.status(200).json({ code: 200, message: '添加购物车商品成功', data: null });
    }
});

// 删除购物车中的某个商品
router.post('/delete', async (req, res) => {
    const { userId, goodsId } = req.body;

    // 查询用户是否存在
    const checkUserQuery = 'SELECT * FROM sys_user WHERE user_id = ?';
    const checkUserRes = await executeQueryAsync(checkUserQuery, [userId]);

    if (checkUserRes.results.length === 0) {
        return res.status(400).json({ code: 600101, message: '用户不存在', data: null });
    }

    // 查询商品是否存在
    const checkGoodsQuery = 'SELECT * FROM goods WHERE goods_id = ?';
    const checkGoodsRes = await executeQueryAsync(checkGoodsQuery, [goodsId]);

    if (checkGoodsRes.results.length === 0) {
        return res.status(400).json({ code: 800101, message: '商品不存在', data: null });
    }

    // 查询购物车中是否存在该用户的该商品
    const checkCartQuery = 'SELECT * FROM cart WHERE user_id = ? AND goods_id = ?';
    const checkCartRes = await executeQueryAsync(checkCartQuery, [userId, goodsId]);

    if (checkCartRes.results.length === 0) {
        return res.status(400).json({ code: 700102, message: '购物车中不存在该商品', data: null });
    }

    // 删除购物车中的该商品
    const deleteQuery = 'DELETE FROM cart WHERE user_id = ? AND goods_id = ?';
    await executeQueryAsync(deleteQuery, [userId, goodsId]);

    res.status(200).json({ code: 200, message: '删除购物车商品成功', data: null });
});


module.exports = router;
